#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <unistd.h>

static const int defaultSize = 3;
using task_t = std::function<void()>;
class ProcessPool
{
public:
    ProcessPool(task_t t, int n = defaultSize)
        : _process(n), _t(t)
    {
        InitPool();
    }
    void InitPool()
    {
        std::vector<pid_t> wfds; 
        for (int i = 0; i < _process.size(); i++)
        {
            int fd[2] = {0};
            pid_t id = fork();
            if (id == 0)
            {
                // 关闭写端
                close(fd[1]);
                dup2(fd[0], 0);
                if(!wfds.empty())
                {
                    for (const auto& fd : wfds)
                        ::close(fd);
                }
                _t();
            }
            // 关闭读端
            close(fd[0]);
            wfds.emplace_back(fd[1]);

            _process[i] = id;
        }
    }
    
    ~ProcessPool()
    {
    }

private:
    std::vector<pid_t> _process;
    task_t _t;
};